var controller = {};

controller.install = {
		
	init : function () {
		
	    view.common.renderInstall();
	}
};

controller.preview = {
		
		init : function () {
			
			view.common.renderPreview();
		}
};

controller.profile = {
		
		init : function () {
			
			view.profile.init();
		},
		
		listWishes : function(turn) {
			
			model.request.getUserWishes(owner.id, function() {
				
				var data = model.storage.wishes.getList(owner.id, turn);
				view.profile.renderWishes(data);
				
			})
		}
		
};

controller.canvas = {
			
	init : function () {
		
		$("body").prepend(template.canvas.maincontent); 
		
		$("#tabs").tabs({
			show : function(event, ui) {
				
				switch(ui.index) {
				case 0:
					controller.canvas.showTagCloud();
					controller.canvas.listSelectedWishes("newest");
					break;
				case 1:
					controller.canvas.listOwnWishes();
					break;
				case 2:
					model.os.fetchFriends(function(data) {
						view.canvas.renderFriendlist(data);
					});
					break;
				case 3:
					controller.canvas.listReserves();
					break;
				}
				
				$("#content").show();
				$("#wish-view").hide();
				$("#system-message").hide();
			}
		});
		
		gadgets.window.adjustHeight(550);
	},
	
	listOwnWishes : function(turn) {
		
		model.request.getUserWishes(viewer.id, function() {
			
			var data = model.storage.wishes.getList(viewer.id, turn);
			
			view.canvas.renderOwnWishes(data);
			
			//if (data.total == 0) {
			//	view.canvas.renderWishAdd();
			//}
		})
	},
	
	listSelectedWishes : function(type, turn) {
		
    	var method = type;
    	var params = {};
    	
    	if (type.indexOf("_") != -1) {
    		var parts = type.split("_");
    		method = parts[0];
    		params = {"filter" : parts[1]}
    	}
		
		model.request.getSelectedWishes(method, params, function() {
			
			var data = model.storage.wishes.getList(type, turn);
			
			data.title = utils.getText('canvas.selection.' + method);
			
			if (!utils.empty(params.filter)) data.title += ' "' + params.filter + '"';
			
			data.type = type;
			
			view.canvas.renderSelectedWishes(data);
		})
	},

	listFriendWishes : function(user_id, turn) {
		
		model.request.getUserWishes(user_id, function () {
			
			var data = model.storage.wishes.getList(user_id, turn);
			
			data.owner_name = model.storage.users.get(user_id)["name"];
			
			view.canvas.renderFriendWishes(data);			
		});
	},

	listUserWishes : function(user_id, turn) {
		
		model.request.getUserWishes(user_id, function () {
			
			var data = model.storage.wishes.getList(user_id, turn);
			
			model.os.fetchUsers(user_id, function(users) {

				data.owner_name = users[data.owner_id].name;
				
				view.canvas.renderUsersWishes(data);				
			});
					
		});
	},
	
	addWish : function(callback) {
		var params = utils.getFieldsData("field-wish-add");
		
		if (params.title.length < 3) {
			view.common.showErrorDialog(utils.getText("error.title_short"), function() {$("#form-add-title").focus()});
			return;
		}
		
		if (params.category_id == "") {
			view.common.showErrorDialog(utils.getText("error.no_category"), function() {$("#form-add-category").focus()});
			return;
		}
		
		model.request.addWish(params, function(data) {
			
			controller.canvas.listOwnWishes("rewind");
			if ($.isFunction(callback)) callback();
			// post feed entry
			var params = {"message" : viewer.name + utils.getText("feed.post_body") + ': "' + data.title + '"',
						  "title" : utils.getText("feed.post_title")};
			model.request.postToFeed(params);
		});		
	},
	
	editWish : function(wish_id, callback) {
		
		var params = utils.getFieldsData("field-wish-edit");
		
		if (params.title.length < 3) {
			view.common.showErrorDialog(utils.getText("error.title_short"), function() {$("#form-edit-title").focus()});
			return;
		}
		
		if (utils.empty(params.category_id)) {
			view.common.showErrorDialog(utils.getText("error.no_category"), function() {$("#form-edit-category").focus()});
			return;
		}
		
		model.request.editWish(params, controller.canvas.listOwnWishes);
		
		if ($.isFunction(callback)) callback();
	},
	
	removeWish : function(wish_id) {
		var params = {'wish_id' : wish_id}
		
		model.request.removeWish(params, controller.canvas.listOwnWishes);				
	},
	
	showWishEdit : function(wish_id) {
		var data = model.storage.wishes.get(wish_id);
				
		view.canvas.renderWishEdit(data);				
	},
	
	showWishCopy : function(wish_id) {
		
		$("#tabs").tabs("select", 1);
		
		var data = model.storage.wishes.get(wish_id);
		
		view.canvas.renderWishAdd(data);				
	},
	
	likeWish : function(wish_id) {
		model.request.likeWish(wish_id, function(success) {
			
			var message = success ?  utils.getText("notice.wish_liked") : utils.getText("error.liked"); 
			view.common.showMessage(message);
			
			if (!success) return;
			
			var wish = model.storage.wishes.get(wish_id);
			
			model.os.sendMessage(wish.owner_id, gt("message.liked.title", {"name" : viewer.name}), gt("message.liked.body", {"title" : wish.title, "name" : viewer.name}));
		});			
	},
	
	showWish : function(wish_id) {
		
		var data = model.storage.wishes.get(wish_id);
		
		model.os.fetchUsers(data.owner_id, function(users) {

			data.owner_name = users[data.owner_id].name;
			
			view.canvas.renderWish(data);				
		});
	},
	
	listLikers : function(user_ids) {
		
		model.os.fetchUsers(user_ids, function(users) {

			view.canvas.renderLikers(users);			
		}, {"noblock" : true});
	},
	
	listReserves : function(turn) {
		
		model.request.getReserves(function (data) {
			
			view.canvas.renderReserves(data);			
		});
	},
	
	addReserve : function(user_id, wish_id) {
		model.request.addReserve(wish_id, function(data) {
			
			var wish = model.storage.wishes.get(wish_id);
			
			model.os.sendMessage(wish.owner_id, gt("message.reserved.title"), gt("message.reserved.body", {"title" : wish.title}));
			
			var data = model.storage.wishes.getList(user_id);
			view.canvas.renderFriendWishes(data);		
		});
	},
	
	showTagCloud : function() {
		
		if (!utils.empty(model.storage.tag_cloud)) {
			view.canvas.renderTagCloud(model.storage.tag_cloud);
			return;
		}
		
		$.getJSON(config.backend_url + "wish/tags?viewer_id=" + viewer.id + "&callback=?", {}, 
			function(data) {
			model.storage.tag_cloud = data;
			view.canvas.renderTagCloud(data);
		});			
	},
	
	removeReserve : function(wish_id) {
		model.request.removeReserve(wish_id, function(data) {

			view.canvas.renderReserves(data);
		});			
	}
	
};

var boot = function() {
	
    var req = opensocial.newDataRequest();
    var params = {};

    model.os.fetchUsers(["VIEWER", "OWNER"], function(data) {
    	
    	switch (currentView) {
    	case 'profile':
    		controller.profile.init();
    		break;
    	case 'preview':
    		controller.preview.init();
    		break;
    	case 'group':
    		controller.install.init();
    		break;
    	default:
    		
    		if (viewer.id == null) {
    			controller.install.init();
    		} else {
    			controller.canvas.init();
    		}
    	
    	break;
    	};
    });
};

